package cn.qh.seasonal.controller;

import cn.qh.seasonal.entity.Factory;
import cn.qh.seasonal.entity.Factoryseasonality;
import cn.qh.seasonal.entity.Shift;
import cn.qh.seasonal.service.FactoryService;
import cn.qh.seasonal.util.PageUtil;
import cn.qh.seasonal.vo.CommonResponse;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import cn.qh.seasonal.dto.FactoryseasonalityDTO;
import cn.qh.seasonal.service.FactoryseasonalityService;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import io.swagger.annotations.ApiImplicitParam;
import org.springframework.beans.BeanUtils;

/**
 * 控制层
 * 控制层
 **/
@RestController
@RequestMapping("factoryseasonality")
@Api
public class FactoryseasonalityController {
    /**
     * 服务对象
     */
    @Resource
    private FactoryseasonalityService factoryseasonalityService;

    @Resource
    private FactoryService factoryService;

    /**
     * 分页查询所有数据
     *
     * @param current 分页对象
     * @return 所有数据
     */
    @GetMapping("/list")
    @ApiOperation(value = "分页查询所有数据", produces = "application/json;charset=utf-8",
            httpMethod = "GET", response = CommonResponse.class)
    @ApiImplicitParam(name = "current", value = "当前页", paramType = "query", dataType = "int")
    @CrossOrigin
    public Page<Factoryseasonality> selectAll(@RequestParam(defaultValue = "1") int current) {
        Page page = new Page();
        page.setCurrent(current);
        page.setSize(3);
        this.factoryseasonalityService.page(page);
        return page;
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    @CrossOrigin
    @ApiOperation(value = "通过主键查询单条数据", produces = "application/json;charset=utf-8",
            httpMethod = "GET", response = CommonResponse.class)
    @ApiImplicitParam(name = "id", value = "主键", paramType = "path", dataType = "int")
    public Factoryseasonality selectOne(@PathVariable Serializable id) {
        return this.factoryseasonalityService.getById(id);
    }

    /**
     * 新增数据
     *
     * @param factoryseasonalityDTO 实体对象
     * @return 新增结果
     */
    @PostMapping("/insert")
    @ApiOperation(value = "新增", produces = "application/json;charset=utf-8",
            httpMethod = "POST", response = CommonResponse.class)
    @CrossOrigin
    public boolean insert(@RequestBody FactoryseasonalityDTO factoryseasonalityDTO) {

        Factoryseasonality factoryseasonality = new Factoryseasonality();

        BeanUtils.copyProperties(factoryseasonalityDTO, factoryseasonality);

        Date startTime = factoryseasonalityDTO.getStartTime();
        Date endTime = factoryseasonalityDTO.getEndTime();

        Factoryseasonality b = factoryseasonalityService.selectFactoryId(factoryseasonalityDTO.getSeasonalId());
        if (b != null) {
            return false;
        } else if (startTime.after(endTime)) {
            System.out.println("实践出错！时间！！！！！");
            return false;
        } else {
            Calendar nowTime = Calendar.getInstance();
            Date time = nowTime.getTime();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String time1 = sdf.format(time);
            String time2 = sdf.format(startTime);
            String time3 = sdf.format(endTime);
            String newYear = time1.substring(0, 4);
            String oldYear = time2.substring(0, 4);
            String oldYear2 = time3.substring(0, 4);
            String time4 = sdf.format(startTime);
            String time5 = sdf.format(endTime);
            String oldMeth1 = time4.substring(5, 7);
            String oldMeth2 = time5.substring(5, 7);
            //秋季
            String meth[] = {"09", "10", "11"};
            //春季
            String meth1[] = {"03", "04", "05"};
            //夏季
            String meth2[] = {"06", "07", "08"};
            //冬季
            String meth3[] = {"12", "01", "02"};
            if (!oldYear.equals(newYear) && oldYear2.compareTo(newYear) > 0 || !oldYear.equals(newYear) && !oldYear2.equals(newYear)) {
                System.out.println("进入第一个判断！！！！！！");
                return false;
            } else {
                //如果是2就是秋季
                if (factoryseasonalityDTO.getSeasonalId() == 2) {
                    System.out.println("进入方法！！！！！！！");
                    if (!oldMeth1.equals(meth[0])) {
                        System.out.println("不是九月");
                    } else {
                        factoryseasonality.setAliaseName("秋季" + newYear);
                        return factoryseasonalityService.save(factoryseasonality);
                    }
                    if (!oldMeth1.equals(meth[1])) {
                        factoryseasonality.setAliaseName("秋季" + newYear);
                        System.out.println("不是10月");
                    } else {
                        factoryseasonality.setAliaseName("秋季" + newYear);
                        return factoryseasonalityService.save(factoryseasonality);
                    }
                    if (!oldMeth1.equals(meth[2])) {
                        System.out.println("不是11月");
                    } else {
                        factoryseasonality.setAliaseName("秋季" + newYear);
                        return factoryseasonalityService.save(factoryseasonality);
                    }
                }
                if (factoryseasonalityDTO.getSeasonalId() == 3) {
                    System.out.println("进入方法！！！！！！");
                    if (!oldMeth1.equals(meth3[0])) {
                        System.out.println("不是12月");
                    } else {
                        factoryseasonality.setAliaseName("冬季" + newYear);
                        return factoryseasonalityService.save(factoryseasonality);
                    }
                    if (!oldMeth1.equals(meth3[1])) {
                        System.out.println("不是1月");
                    } else {
                        factoryseasonality.setAliaseName("冬季" + newYear);
                        return factoryseasonalityService.save(factoryseasonality);
                    }
                    if (!oldMeth1.equals(meth3[2])) {
                        System.out.println("不是2月");
                    } else {
                        factoryseasonality.setAliaseName("冬季" + newYear);
                        return factoryseasonalityService.save(factoryseasonality);
                    }
                }
                if (factoryseasonalityDTO.getSeasonalId() == 4) {
                    System.out.println("进入方法！！！！！！");
                    if (!oldMeth1.equals(meth2[0])) {
                        System.out.println("不是6月");
                    } else {
                        factoryseasonality.setAliaseName("夏季" + newYear);
                        return factoryseasonalityService.save(factoryseasonality);
                    }
                    if (!oldMeth1.equals(meth2[1])) {
                        System.out.println("不是7月");
                    } else {
                        factoryseasonality.setAliaseName("夏季" + newYear);
                        return factoryseasonalityService.save(factoryseasonality);
                    }
                    if (!oldMeth1.equals(meth2[2])) {
                        System.out.println("不是8月");
                    } else {
                        factoryseasonality.setAliaseName("夏季" + newYear);
                        return factoryseasonalityService.save(factoryseasonality);
                    }
                }
                if (factoryseasonalityDTO.getSeasonalId() == 1) {
                    System.out.println("进入方法！！！！！！");
                    if (!oldMeth1.equals(meth1[0])) {
                        System.out.println("不是3月");
                    } else {
                        factoryseasonality.setAliaseName("春季" + newYear);
                        return factoryseasonalityService.save(factoryseasonality);
                    }
                    if (!oldMeth1.equals(meth1[1])) {
                        System.out.println("不是4月");
                    } else {
                        factoryseasonality.setAliaseName("春季" + newYear);
                        return factoryseasonalityService.save(factoryseasonality);
                    }
                    if (!oldMeth1.equals(meth1[2])) {
                        System.out.println("不是5月");
                    } else {
                        factoryseasonality.setAliaseName("春季" + newYear);
                        return factoryseasonalityService.save(factoryseasonality);
                    }
                }

            }
            return true;
        }
    }

    /**
     * 修改数据
     *
     * @param factoryseasonalityDTO 实体对象
     * @return 修改结果
     */
    @PutMapping("/update")
    @ApiOperation(value = "修改信息", produces = "application/json;charset=utf-8",
            httpMethod = "PUT", response = CommonResponse.class)
    @CrossOrigin
    public boolean update(@RequestBody FactoryseasonalityDTO factoryseasonalityDTO) {
        Factoryseasonality factoryseasonality = new Factoryseasonality();
        BeanUtils.copyProperties(factoryseasonalityDTO, factoryseasonality);


        Date startTime = factoryseasonalityDTO.getStartTime();
        Date endTime = factoryseasonalityDTO.getEndTime();

        Factoryseasonality b = factoryseasonalityService.selectFactoryId(factoryseasonalityDTO.getSeasonalId());
        if (b != null) {
            return false;
        } else if (startTime.after(endTime)) {
            System.out.println("实践出错！时间！！！！！");
            return false;
        } else {
            Calendar nowTime = Calendar.getInstance();
            Date time = nowTime.getTime();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String time1 = sdf.format(time);
            String time2 = sdf.format(startTime);
            String time3 = sdf.format(endTime);
            String newYear = time1.substring(0, 4);
            String oldYear = time2.substring(0, 4);
            String oldYear2 = time3.substring(0, 4);
            String time4 = sdf.format(startTime);
            String time5 = sdf.format(endTime);
            String oldMeth1 = time4.substring(5, 7);
            String oldMeth2 = time5.substring(5, 7);
            //秋季
            String meth[] = {"09", "10", "11"};
            //春季
            String meth1[] = {"03", "04", "05"};
            //夏季
            String meth2[] = {"06", "07", "08"};
            //冬季
            String meth3[] = {"12", "01", "02"};
            if (!oldYear.equals(newYear) && oldYear2.compareTo(newYear) > 0 || !oldYear.equals(newYear) && !oldYear2.equals(newYear)) {
                System.out.println("进入第一个判断！！！！！！");
                return false;
            } else {
                //如果是2就是秋季
                if (factoryseasonalityDTO.getSeasonalId() == 2) {
                    System.out.println("进入方法！！！！！！！");
                    if (!oldMeth1.equals(meth[0])) {
                        System.out.println("不是九月");
                    } else {
                        factoryseasonality.setAliaseName("秋季" + newYear);
                        return this.factoryseasonalityService.updateById(factoryseasonality);
                    }
                    if (!oldMeth1.equals(meth[1])) {
                        factoryseasonality.setAliaseName("秋季" + newYear);
                        System.out.println("不是10月");
                    } else {
                        factoryseasonality.setAliaseName("秋季" + newYear);
                        return this.factoryseasonalityService.updateById(factoryseasonality);
                    }
                    if (!oldMeth1.equals(meth[2])) {
                        System.out.println("不是11月");
                    } else {
                        factoryseasonality.setAliaseName("秋季" + newYear);
                        return this.factoryseasonalityService.updateById(factoryseasonality);
                    }
                }
                if (factoryseasonalityDTO.getSeasonalId() == 3) {
                    System.out.println("进入方法！！！！！！");
                    if (!oldMeth1.equals(meth3[0])) {
                        System.out.println("不是12月");
                    } else {
                        factoryseasonality.setAliaseName("冬季" + newYear);
                        return this.factoryseasonalityService.updateById(factoryseasonality);
                    }
                    if (!oldMeth1.equals(meth3[1])) {
                        System.out.println("不是1月");
                    } else {
                        factoryseasonality.setAliaseName("冬季" + newYear);
                        return this.factoryseasonalityService.updateById(factoryseasonality);
                    }
                    if (!oldMeth1.equals(meth3[2])) {
                        System.out.println("不是2月");
                    } else {
                        factoryseasonality.setAliaseName("冬季" + newYear);
                        return this.factoryseasonalityService.updateById(factoryseasonality);
                    }
                }
                if (factoryseasonalityDTO.getSeasonalId() == 4) {
                    System.out.println("进入方法！！！！！！");
                    if (!oldMeth1.equals(meth2[0])) {
                        System.out.println("不是6月");
                    } else {
                        factoryseasonality.setAliaseName("夏季" + newYear);
                        return this.factoryseasonalityService.updateById(factoryseasonality);
                    }
                    if (!oldMeth1.equals(meth2[1])) {
                        System.out.println("不是7月");
                    } else {
                        factoryseasonality.setAliaseName("夏季" + newYear);
                        return this.factoryseasonalityService.updateById(factoryseasonality);
                    }
                    if (!oldMeth1.equals(meth2[2])) {
                        System.out.println("不是8月");
                    } else {
                        factoryseasonality.setAliaseName("夏季" + newYear);
                        return this.factoryseasonalityService.updateById(factoryseasonality);
                    }
                }
                if (factoryseasonalityDTO.getSeasonalId() == 1) {
                    System.out.println("进入方法！！！！！！");
                    if (!oldMeth1.equals(meth1[0])) {
                        System.out.println("不是3月");
                    } else {
                        factoryseasonality.setAliaseName("春季" + newYear);
                        return this.factoryseasonalityService.updateById(factoryseasonality);
                    }
                    if (!oldMeth1.equals(meth1[1])) {
                        System.out.println("不是4月");
                    } else {
                        factoryseasonality.setAliaseName("春季" + newYear);
                        return this.factoryseasonalityService.updateById(factoryseasonality);
                    }
                    if (!oldMeth1.equals(meth1[2])) {
                        System.out.println("不是5月");
                    } else {
                        factoryseasonality.setAliaseName("春季" + newYear);
                        return this.factoryseasonalityService.updateById(factoryseasonality);
                    }
                }

            }
            return true;
        }
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除结果
     */
    @DeleteMapping("/del/{id}")
    @ApiOperation(value = "根据主键删除信息", produces = "application/json;charset=utf-8",
            httpMethod = "DELETE", response = CommonResponse.class)
    @ApiImplicitParam(name = "id", value = "主键", paramType = "path", dataType = "int")
    @CrossOrigin
    public boolean delete(@PathVariable Serializable id) {
        return this.factoryseasonalityService.removeById(id);
    }


    //查询第一条
    @GetMapping("/All")
    public PageUtil<Factoryseasonality> list(@RequestParam(name = "factoryId", defaultValue = "0") int factoryId, @RequestParam(name = "seasonalId", defaultValue = "0") int seasonalId, @RequestParam(name = "currentPage", defaultValue = "1") int currentPage) {
        Factoryseasonality factoryseasonality = new Factoryseasonality();
        List<Factory> list = factoryService.list();
        PageUtil<Factoryseasonality> pageUtil =
                factoryseasonalityService.selectFactory(factoryId, seasonalId, currentPage);

        Factory byId = factoryService.getById(factoryId);
        if (byId != null) {
            if (byId.getType().equals("1")) {
                PageUtil<Factoryseasonality> pageUtil3 =
                        factoryseasonalityService.selectFactory(factoryId, seasonalId, currentPage);
                return pageUtil3;
            } else {
                Factory byId1 = factoryService.getById(byId.getPId());
                PageUtil<Factoryseasonality> pageUtil2 =
                        factoryseasonalityService.selectFactory(byId1.getId(), seasonalId, currentPage);
                return pageUtil2;
            }
        } else {
            return pageUtil;
        }
    }
}
